package karileet.meeting.profile.model.ui;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import karileet.meeting.profile.MainActivity;
import karileet.meeting.profile.ProfileState;
import karileet.meeting.profile.service.ActivationService;
import karileet.meeting.profile.service.DisablingService;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.widget.TimePicker;

public class ScheduleDialogFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
	private String type;
	private ProfileState state;
	private long time;
	
	public void init(String type, ProfileState ps) {
		this.type = type;
		state = ps;
		if (type.equals("start")) {
			state.isScheduleStartEnabled();
			time = state.getScheduleStart();
		} else {
			state.isScheduleStopEnabled();
			time = state.getScheduleStop();
		}
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Create a new instance of TimePickerDialog and return it
		Date date = new Date(time);
		SimpleDateFormat hourformat = new SimpleDateFormat("H");
		SimpleDateFormat minuteformat = new SimpleDateFormat("mm");
		int hour = Integer.parseInt(hourformat.format(date));
		int minute = Integer.parseInt(minuteformat.format(date));
		TimePickerDialog tpd = new TimePickerDialog(getActivity(), this, hour, minute,
                true);
        return tpd;
	}
	
	private void startService(int h, int m, String type) 
	{
		Intent alarmIntent = null;
		long oneDay = 86400000;
		long startTime = 0;
		if (type.equals("start")) {
			alarmIntent = new Intent(getActivity(), ActivationService.class);
		} else {
			alarmIntent = new Intent(getActivity(), DisablingService.class);
		}
		 
		alarmIntent.putExtra("scheduleType", type);
		PendingIntent pendingAlarmIntent = PendingIntent.getService(getActivity(), 0, alarmIntent, 0);
		
		AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
		
		Calendar AlarmCal = Calendar.getInstance();
		AlarmCal.setTimeInMillis(System.currentTimeMillis());
		AlarmCal.set(Calendar.HOUR_OF_DAY, h);  // set user selection
		AlarmCal.set(Calendar.MINUTE, m);        // set user selection
		AlarmCal.set(Calendar.SECOND, 0);
		
		//adjust start time, so that service wont start to past time
		if (AlarmCal.getTimeInMillis() < System.currentTimeMillis()) {
			Log.d(MainActivity.APP, "Padding time one day!");
			startTime = AlarmCal.getTimeInMillis() + oneDay; 
		} else {
			Log.d(MainActivity.APP, "Using original time!");
			startTime = AlarmCal.getTimeInMillis(); 
		}
		
		if (type.equals("start")) {
			state.setScheduleStart(startTime);
		} else {
			state.setScheduleStop(startTime);
		}
//		alarmManager.set(AlarmManager.RTC_WAKEUP, startTime, pendingAlarmIntent);
		alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
		           startTime, AlarmManager.INTERVAL_DAY,
		           pendingAlarmIntent);
		//update view with latest times
		Intent mainintent = new Intent(getActivity(), MainActivity.class);
		startActivity(mainintent);
	}

	@Override
	public void onTimeSet(TimePicker tp, int h, int m) {
		startService(h, m, type);
	}

	
}
